﻿/* ------------------------------------------- INDEX PAGE EVENTS -------------------------------------------------------- */
/* ------------------------------------------- INDEX PAGE EVENTS -------------------------------------------------------- */
/* ------------------------------------------- INDEX PAGE EVENTS -------------------------------------------------------- */

$(function () {

    $(".requesttoken input").removeAttr("disabled");


    /* ----------------------------------------------------------------- */
    /*               Dropdown list changed function                      */
    /* ----------------------------------------------------------------- */
    $("select#ddlProject").change(function (evt) {
        GetProjectData();
    });

    /* ----------------------------------------------------------------- */
    /*               Button Set Lead Engineer Click                      */
    /* ----------------------------------------------------------------- */
    $("#btnSetLeadEngineer").click(function (evt) {
        if ($("select#ddlLeadEngineers").val() != "-1" && $("select#ddlLeadEngineers").val() != "" && $("select#ddlLeadEngineers").val() != null) {
            if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {

                var leadEngValue = $("#ddlLeadEngineers").val();
                var leadEngName = $('.chzn-single-with-drop', $("#ddlLeadEngineers")).text();
                var projectName = $('.chzn-single-with-drop', $("#ddlProject")).text();
                var projectId = $("#ddlProject").val();

                SetLeadEngineer(projectId, leadEngValue, projectName, leadEngName);
            }
            else {
                alert('Please select one project');
            }
        }
        else {
            alert('Please select one user to assign as lead engineer');
        }
    });

    /* ----------------------------------------------------------------- */
    /*               Button Add New Task Click                           */
    /* ----------------------------------------------------------------- */
    $("#btnAddNewTask").click(function (evt) {
        if ($("select#ddlUsers").val() != "-1" && $("select#ddlUsers").val() != "" && $("select#ddlUsers").val() != null) {
            if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {

                var projectId = $("#ddlProject").val();
                var userId = $("#ddlUsers").val();
                var listUserId = "";
                $.each(userId, function (key, value) {
                    listUserId = listUserId + ',' + value;
                });

                var taskName = $("#txtTaskName").val();
                // alert(projectId +  userId +  taskName);
                var startDate = $('#inputStartDate').val() + " " + $('#inputStartTime').val();
                var endDate = $('#inputEndDate').val() + " " + $('#inputEndTime').val();

                // Add new project task
                AddNewProjectTask(taskName, projectId, listUserId, startDate, endDate);
            }
            else {
                alert('Please select one project');
            }
        }
        else {
            alert('Please select one user to assign this task');
        }
    });

    /* ----------------------------------------------------------------- */
    /*               Button Save Task Click                           */
    /* ----------------------------------------------------------------- */
    $("#btnSaveTask").click(function (evt) {
        if ($("select#ddlUsers").val() != "-1" && $("select#ddlUsers").val() != "" && $("select#ddlUsers").val() != null) {
            if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {

                var projectId = $("#ddlProject").val();
                var userId = $("#ddlUsers").val();
                var taskId = $("#txtTaskId").val();

                var listUserId = "";
                $.each(userId, function (key, value) {
                    listUserId = listUserId + ',' + value;
                });

                var taskName = $("#txtTaskName").val();
                // alert(projectId +  userId +  taskName);
                var startDate = $('#inputStartDate').val() + " " + $('#inputStartTime').val();
                var endDate = $('#inputEndDate').val() + " " + $('#inputEndTime').val();


                // Add new project task
                EditProjectTask(taskId, taskName, projectId, listUserId, startDate, endDate);

            }
            else {
                alert('Please select one project');
            }
        }
        else {
            alert('Please select one user to assign this task');
        }
    });

    /* ----------------------------------------------------------------- */
    /*               Button Delete Task Click                           */
    /* ----------------------------------------------------------------- */
    $("#btnDeleteTask").click(function (evt) {

        var taskId = $("#txtTaskId").val();

        if (taskId != "-1" && taskId != "" && taskId != null) {
            var c = confirm('Are you sure you want to delete this task ?');
            if (c == true) {
                DeleteProjectTask(taskId);
            }
        }
        else {
            alert('Please select one task first');
        }
    });
});

/* ------------------------------------------- TASK GRID EVENTS -------------------------------------------------------- */
/* ------------------------------------------- TASK GRID EVENTS -------------------------------------------------------- */
/* ------------------------------------------- TASK GRID EVENTS -------------------------------------------------------- */
$(function () {

    $('#ajaxGridTask tbody tr').click(function () {

        $('#ajaxGridTask tbody tr').each(function () {
            $(this).removeClass('selected');
        });
        $(this).addClass('selected');

        // Get task id
        var currentTaskId = $(this).find('td:first').text();

        // Load task to edit
        LoadTaskToEdit(currentTaskId);

    });
});

/* ------------------------------------------- TASK GRID EVENTS -------------------------------------------------------- */